package com.weilan.ark3.controller;


import com.alibaba.excel.EasyExcel;
import com.weilan.ark3.common.Result;
import com.weilan.ark3.entity.ModbusConf;
import com.weilan.ark3.excel.ExcelModelListener;
import com.weilan.ark3.exception.CustomException;
import com.weilan.ark3.mapper.ModbusConfMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @description Excel操作类
 * @date 2023/3/29 19:11
 */
@CrossOrigin
@RestController
@Api(tags = "Excel操作类")
@RequestMapping("/weilan/excel")
public class ExcelController {

    @Autowired
    private ModbusConfMapper modbusConfMapper;

    @ApiOperation("excel读取入库")
    @PostMapping("readExcel")
    public Result<Object> readExcel(MultipartFile file, String moduleName, Integer sheet, Integer row) {
        try {
            //文件输入流
            InputStream is = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(is, ModbusConf.class, new ExcelModelListener(modbusConfMapper,moduleName)).sheet(sheet).headRowNumber(row).doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(20001, "导入点表信息excel失败");
        }
        return Result.success().message("加载excel点表成功");
    }
}
